package oracle_sql_option

import (
	"log"

	"github.com/syndtr/goleveldb/leveldb"
)

type LevelData struct {
	LevelKey   []byte
	LevelValue []byte
}

func OpenLevelDb(datapath string) (*leveldb.DB, error) {
	//创建并打开数据库
	db, err := leveldb.OpenFile(datapath, nil)
	if err != nil {
		return nil, err
	}
	return db, nil
}

func BatchDataInput(db *leveldb.DB, data []LevelData) {
	//批量写入数据
	batch := new(leveldb.Batch)
	for _, val := range data {
		batch.Put(val.LevelKey, val.LevelValue)
	}
	db.Write(batch, nil)
}

func SeekData(key string, db *leveldb.DB) {
	//查找数据
	iter := db.NewIterator(nil, nil)
	defer iter.Release()

	for ok := iter.Seek([]byte(key)); ok; ok = iter.Next() {
		log.Println("key:=", string(iter.Key()), "  ok:=", ok)
	}
	log.Println("false")
}

func SeekAll(db *leveldb.DB) {
	// //循环遍历数据
	log.Println("循环遍历数据")
	iter := db.NewIterator(nil, nil)
	for iter.Next() {
		log.Printf("key:%s, value:%s\n", iter.Key(), iter.Value())
	}
	iter.Release() //释放
}
